package com.zc.finkDemo.service;

import com.zc.finkDemo.entities.RuleInfo;
import com.zc.finkDemo.mapper.RuleMapper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.ConcurrentHashMap;

public class RuleService {

    public static ConcurrentHashMap<String,RuleInfo> cache=new ConcurrentHashMap<>();


    /**
     * 根据规则key获取规则内容
     * 先从本地缓存中获取
     * 如果获取不到再从数据库中查询 并放入缓存
     * @param key
     * @return
     */
    public static RuleInfo getRule(String key) throws SQLException {
        RuleInfo ruleInfo = cache.get(key);
        if (ruleInfo==null){
            ruleInfo=RuleMapper.getRule(key);
            cache.put(key,ruleInfo);
        }
        return ruleInfo;
    }
}
